﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Tank : Ship
{
    public GameObject m_Bullet1Perfab;
    public GameObject m_Bullet2Perfab;
    public GameObject m_Bullet3Perfab;

    List<GameObject> m_BulletList = new List<GameObject>();
    string SoundPath = "Perfabs/Audio/Pong";

    float StructExpand = 150;
    float ElectricExpand = 200;
    protected new void Start()
    {
        base.Start();
        m_BulletList.Add(m_Bullet1Perfab);
        m_BulletList.Add(m_Bullet2Perfab);
        m_BulletList.Add(m_Bullet3Perfab);
    }

    public override bool LaunchSkill()
    {
        if (m_ShipStruct.Electric < ElectricExpand ||
            m_ShipStruct.StructurePoint < StructExpand)
        {
            MessageShow.Instance.Messaage("能量或结构值不足。");
            return false;
        }

        if (!base.LaunchSkill())
            return false;


        float baseErrorRange = 50;
        var horizontalDir = Vector3.Cross(transform.up, new Vector3(0, 0, 1));
        var offset = horizontalDir * Random.Range(0, 0.1f);
        var positionB = transform.position + offset;

        for(int i=0; i< 20; ++i)
        {
             var bulletObj = Instantiate(m_BulletList[Random.Range(0, m_BulletList.Count)]);
            if (UtilsGame.IsOnline() && UtilsGame.IsServer(this.gameObject))
            {
                UtilsGame.SpawnInServer(bulletObj.gameObject);
            }
            var bullet = bulletObj.GetComponent<Bullet>();
            bullet.InitParameter(positionB, 
                Quaternion.AngleAxis(Random.Range(0, baseErrorRange), new Vector3(0, 1, 1)) * transform.up, 
                Random.Range(5, 10), 
                Random.Range(7, 10), 
                Random.Range(10, 80),
                "",
                Weapon.DamageType.TYPE_KINETIC,
                this, 
                null);
        }
        AddForce(-transform.up, 1500);
        SoundsPool.get().PlaySound(SoundPath, transform.position);
        m_ShipStruct.StructurePoint -= 200;
        m_ShipStruct.Electric -= 200;

        return true;
    }
}
